package com.daluzy.chmanager.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.daluzy.chmanager.MyApplication;
import com.daluzy.chmanager.R;
import com.daluzy.chmanager.activity.AddCommentActivity;
import com.daluzy.chmanager.activity.TestPointInfoActivity;
import com.daluzy.chmanager.bean.Record;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

public class RecordAdapter extends RecyclerView.Adapter<RecordAdapter.MyViewHolder> {



    private Context context;
    private List<Record> myData = new ArrayList<>();

    public RecordAdapter(Context context, List<Record> myData) {
        this.context = context;
        this.myData = myData;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_record_item_daluzy, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        Record record = myData.get(position);
        holder.tvRecordArea.setText("所在区域：" + record.getRecord_area());
        holder.tvRecordTitle.setText(record.getRecord_title());
        holder.tvRecordTestPoint.setText("测试点：" + record.getRecord_test_point());
        holder.tvRecordPerson.setText(record.getRecord_student());

        if (record.getRecord_type().equals("1")) {
            holder.tvRecordType.setText("未维修");
            if (MyApplication.getIntance().getUserType() == 2){
                holder.tvAddComment.setVisibility(View.VISIBLE);
                holder.tvAddComment.setText("去维修");

            }else {
                holder.tvAddComment.setVisibility(View.GONE);
            }

        } else if (record.getRecord_type().equals("2")){
            holder.tvRecordType.setText("已维修");
            if (MyApplication.getIntance().getUserType() == 1){
                holder.tvAddComment.setVisibility(View.VISIBLE);
                holder.tvAddComment.setText("去评价");
            }else {
                holder.tvAddComment.setVisibility(View.GONE);
            }
        }else {
            holder.tvRecordType.setText("已评价");
            holder.tvAddComment.setVisibility(View.GONE);

        }
        holder.tvAddComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (MyApplication.getIntance().getUserType() == 1){
                    //去评价
                    Intent intent = new Intent(context, AddCommentActivity.class);
                    intent.putExtra("record",record);
                    context.startActivity(intent);
                }else if (MyApplication.getIntance().getUserType() == 2){
                    //维修
                    Intent intent = new Intent(context, TestPointInfoActivity.class);
                    intent.putExtra("type",1);
                    intent.putExtra("record",record);
                    context.startActivity(intent);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return myData.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_record_title)
        TextView tvRecordTitle;
        @BindView(R.id.tv_record_area)
        TextView tvRecordArea;
        @BindView(R.id.tv_record_test_point)
        TextView tvRecordTestPoint;
        @BindView(R.id.tv_record_person)
        TextView tvRecordPerson;
        @BindView(R.id.tv_record_type)
        TextView tvRecordType;
        @BindView(R.id.tv_add_comment)
        TextView tvAddComment;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
